import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../routes/routes.dart';
import '../../utils/Tools.dart';

class ServiceSupportPage extends StatelessWidget {
  const ServiceSupportPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          '联系客服',
          style: TextStyle(fontWeight: FontWeight.w500, fontSize: 17, color: Tools.fromHex('#333333')),
        ),
        backgroundColor: Colors.white,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: Container(
        color: Colors.grey[200], // 设置背景颜色
        child: Column(
          children: [
            _buildListTile(
              icon: Icons.headset_mic, // 图标：在线客服
              iconColor: Colors.blue,
              title: '在线客服',
              subtitle: '你的满意我的追求',
              onTap: () {
                // 在线客服点击事件逻辑
                Get.toNamed(Routes.customerService);
              },
            ),
            Divider(height: 1, color: Colors.grey[300]), // 分割线
            _buildListTile(
              icon: Icons.phone, // 图标：联系电话
              iconColor: Colors.orange,
              title: '联系电话',
              subtitle: '4008003629',
              onTap: () async {
                final Uri telUri = Uri(scheme: 'tel', path: '4008003629');
                try {
                  if (await canLaunchUrl(telUri)) {
                    await launchUrl(telUri);
                  } else {
                    if (!context.mounted) return; // 确保 BuildContext 有效
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(content: Text("无法拨打电话")),
                    );
                  }
                } catch (e) {
                  if (!context.mounted) return; // 确保 BuildContext 有效
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text("拨打电话时出错")),
                  );
                  debugPrint("Error launching URL: $e");
                }
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildListTile({
    required IconData icon,
    required Color iconColor,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        color: Colors.white, // 每个模块的背景颜色
        child: Row(
          children: [
            CircleAvatar(
              backgroundColor: iconColor.withOpacity(0.2), // 图标背景颜色
              child: Icon(icon, color: iconColor),
            ),
            const SizedBox(width: 16), // 图标与文字之间的间距
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      color: Colors.black,
                    ),
                  ),
                  const SizedBox(height: 4), // 标题与内容之间的间距
                  Text(
                    subtitle,
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey[600],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
